//
//  AppDelegate.m
//  DECXInStore
//
//  Created by 程强 on 15/12/9.
//  Copyright © 2015年 程强. All rights reserved.
//

#import "AppDelegate.h"
#import "IQKeyboardManager.h"
#import "DECXManager.h"

@interface AppDelegate ()<UIAlertViewDelegate>
@property (strong, nonatomic) DECXManager *manager;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self keyBoardConfig];
    self.manager = [DECXManager new];
    
    return YES;
}

/*
 *配置键盘自动滚动
 */
- (void)keyBoardConfig
{
    //为避免影响目前已有功能 暂时关闭键盘自动滚动
    [[IQKeyboardManager sharedManager] setEnable:YES];
    //关闭键盘顶部悬浮bar
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    //打开点击空白部分收起键盘
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
}

#pragma mark --- 检查版本
- (void)checkAppVersion
{
    [[self.manager checkVersionUpdate]subscribeNext:^(id x) {
        NSDictionary *dic = x;
        NSString *serverVersion = dic[@"version"];
        NSString *isForce = dic[@"isforce"];
        NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        isForce = @"0";
        if ([appVersion compare:serverVersion] == NSOrderedAscending) {
            if (isForce.integerValue == 1) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"检测到有重要版本发布！" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"更新", nil];
                alert.tag = 101;
                [alert show];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"检测到有新版本，确认更新？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
                alert.tag = 102;
                [alert show];
            }
        }
        
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 101) {
        if (buttonIndex == 0) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.decxagri.com/data/Download/store.html"]];
        }
    }else if (alertView.tag == 102){
        if (buttonIndex == 1){
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.decxagri.com/data/Download/store.html"]];
        }
    }
    
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    if (isEnterprise) {
        [self checkAppVersion];
    }
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
